#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2012/02/19

Module with the class that creates a html or csv file for all the combinations
    possible in statistics #2 and #3 in a year due to the size of the files
    for all the years(~300Mb for csv and ~700Mb to the html)

@author: João Vitor Machado Batista nº 5479
'''

from getStatistics import *
import time
import csv

class CreateAllStatistics:

    def getStatistic2All(self, fileType, year):
        '''
            creates a html or csv file for all the combinations
                possible in statistics #2
        '''

        obj = getStatistics()

        teacherList = []
        dicCat = {}
        dicSchool = {}
        dicYear = {}

        databaseContent = obj.getTableByYear(year)

        schools = obj.getSchoolName(databaseContent)

        for school in schools:

            categories = set([])
            bdSchool = []
            
            for line in databaseContent:
                if(school == line[8]):
                    categories.add(line[4])
                    bdSchool.append(line)
                    pass
                pass
                            
            categories = list(categories)
            categories.sort()
                
            for categorie in categories:
                
                teacherList = obj.getTeachersNameByCategoryEstablisment(school, categorie, bdSchool)
                dicCat[categorie] = teacherList
                pass
                            
            dicSchool[school] = dicCat

            pass

        dicYear[year] = dicSchool
        

        if(fileType == 'csv'):
            
            fileName = '../csv/statistic_2_all.csv'
            
            csvFile = open(fileName, 'wb')
            fileStorage = csv.writer(csvFile, delimiter=',')

            fileStorage.writerow(['Statistic 2, All combinations'])
            fileStorage.writerow([])
            pass

        
        if(fileType == 'html'):
            
            fileName = '''html/statistic_2_all.html'''
        
            filehtml = open(fileName, 'wb')

            # string with the html code

            htmlCode1 = '''<!DOCTYPE HTML>
                        <html>
                        <head>
                        <title>{0}</title>
                        <meta name="description" content="{0}" />
                        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                        </head>
                        <body>
                        </br>
                        <div align="center"><h1>{0}</h1></div>
                        </br>
                        '''.format('Statistic 2, All combinations')
            
            # the body of html
            filehtml.write(htmlCode1)
            pass
        

        for year in dicYear:

            for school in dicYear[year]:

                for category in dicYear[year][school]:

                    teachers = dicYear[year][school][category]

                    quantity = len(teachers)

                    if(fileType == 'csv'):
                        fileStorage.writerow(['Year', year])
                        fileStorage.writerow(['School', school.encode('utf-8')])
                        fileStorage.writerow(['Category', category.encode('utf-8')])
                        fileStorage.writerow(['Quantity', quantity])
                        fileStorage.writerow([])
                        fileStorage.writerow(['Teachers List'])
                        
                        for x in range(0, quantity):

                            teacher = teachers[x].encode('utf-8')

                            fileStorage.writerow(['# ' + str(x +1), teacher])
                            pass
                        
                        fileStorage.writerow([])
                        pass
                    
                    if(fileType == 'html'):

                        param = ['Year: ', year, 'School: ', school.encode('utf-8'), 'Category: ', category.encode('utf-8'), 'Quantity: ', quantity]
                        
                        a = 0
                        while( a <= len(param) -1):
                            htmlCode2 = '''<div align="center"><h4>{0}{1}</h4></div>'''.format(param[a], param[a+1])
                            filehtml.write(htmlCode2)
                            a = a + 2
                            pass

                        htmlCode3 = '''</br><div align="center"><h2>Teacher List:</a></h2></div>'''
                        filehtml.write(htmlCode3)

                        for x in range(0, quantity):

                            teacher = teachers[x].encode('utf-8')

                            htmlCode4 = '''<div align="center"><h5>#{0} - {1}</h5></div>'''.format(x+1, teacher)
                            filehtml.write(htmlCode4)
                            
                            pass
                        
                        filehtml.write('</br>')
                        pass
                    
                    pass
                
                pass
            
            pass
        
        if(fileType == 'html'):
            htmlCode5 = '''</body>
                           </html>'''

            filehtml.write(htmlCode5)
            filehtml.close()
            pass
        
        pass

    def getStatistic3All(self, fileType, year):
        '''
            creates a html or csv file for all the combinations
                possible in statistics #3
        '''
        obj = getStatistics()

        profList = []
        yearDic = {}
        degreeDic = {}
        schoolDic = {}
        categoryDic = {}
        degreeType = {'Doctorate Level': "D", 'Masters Level': "M", 'Graduated Level': "L"}
        
        databaseContent = obj.getTableByYear(year)
        
        schools = obj.getSchoolName(databaseContent)

        for degree in degreeType:
            
            for school in schools:

                categories = set([])
                bdSchool = []
            
                for line in databaseContent:
                    if(school == line[8]):
                        categories.add(line[4])
                        bdSchool.append(line)
                        pass
                    pass

                categories = list(categories)
                categories.sort()
                
                for categorie in categories:

                    profList = obj.getTeacherNameByDegreeTypeEstablismentCategory(school, degreeType[degree], categorie, bdSchool)
                    categoryDic[categorie] = profList
                    
                    pass
                
                schoolDic[school] = categoryDic
                pass
            
            degreeDic[degree] = schoolDic
            pass
        
        yearDic[year] = degreeDic


        if(fileType == 'csv'):
            
            fileName = '../csv/statistic_3_all.csv'
            
            csvFile = open(fileName, 'wb')
            fileStorage = csv.writer(csvFile, delimiter=',')

            fileStorage.writerow(['Statistic 3, All combinations'])
            fileStorage.writerow([])
            pass

        
        if(fileType == 'html'):
            
            fileName = '''html/statistic_3_all.html'''
        
            filehtml = open(fileName, 'wb')

            # string with the html code

            htmlCode1 = '''<!DOCTYPE HTML>
                        <html>
                        <head>
                        <title>{0}</title>
                        <meta name="description" content="{0}" />
                        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                        </head>
                        <body>
                        </br>
                        <div align="center"><h1>{0}</h1></div>
                        </br>
                        '''.format('Statistic 3, All combinations')
            
            # the body of html
            filehtml.write(htmlCode1)
            pass 


        for year in yearDic:

            for degree in yearDic[year]:

                for school in yearDic[year][degree]:

                    for category in yearDic[year][degree][school]:

                        teachers = yearDic[year][degree][school][category]

                        quantity = len(teachers)

                        if(fileType == 'csv'):
                            fileStorage.writerow(['Year', year])
                            fileStorage.writerow(['School', school.encode('utf-8')])
                            fileStorage.writerow(['Category', category.encode('utf-8')])
                            fileStorage.writerow(['Degree', degree.encode('utf-8')])
                            fileStorage.writerow(['Quantity', quantity])
                            fileStorage.writerow([])
                            fileStorage.writerow(['Teachers List'])
                            
                            for x in range(0, quantity):

                                teacher = teachers[x].encode('utf-8')

                                fileStorage.writerow(['# ' + str(x +1), teacher])
                                pass
                            
                            fileStorage.writerow([])
                            pass
                        
                        if(fileType == 'html'):

                            param = ['Year: ', year, 'School: ', school.encode('utf-8'),
                                     'Category: ', category.encode('utf-8'),
                                     'Degree', degree.encode('utf-8'), 'Quantity: ', quantity]
                            
                            a = 0
                            while( a <= len(param) -1):
                                htmlCode2 = '''<div align="center"><h4>{0}{1}</h4></div>'''.format(param[a], param[a+1])
                                filehtml.write(htmlCode2)
                                a = a + 2
                                pass

                            htmlCode3 = '''</br><div align="center"><h2>Teacher List:</a></h2></div>'''
                            filehtml.write(htmlCode3)

                            for x in range(0, quantity):

                                teacher = teachers[x].encode('utf-8')

                                htmlCode4 = '''<div align="center"><h5>#{0} - {1}</h5></div>'''.format(x+1, teacher)
                                filehtml.write(htmlCode4)
                                
                                pass
                            
                            filehtml.write('</br>')
                            pass
                        
                        pass
                    
                    pass
                
                pass
            
            pass
        
        if(fileType == 'html'):
            htmlCode5 = '''</body>
                           </html>'''

            filehtml.write(htmlCode5)
            filehtml.close()
            pass
        
        pass
